package com.project.service;

import com.project.dao.DepartmentMapper;
import com.project.entity.Department;
import com.project.entity.StudentInfo;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 13:43
 * @param:
 * @return:
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class DepartmentServiceImpl extends BaseService implements DepartmentService {
    @Autowired
    private  DepartmentMapper departmentMapper;

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ClassService classService;


    @Override
    public int addOne(Department department) {

        return departmentMapper.insertSelective(department);
    }

    @Override
    public int remove(Integer id) {

        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int modify(Department department) {

        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public Department findOne(Integer id) {

        return departmentMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2015-5-4
     * function: 根据schoolId 查找系/部/科室
     *
     * @param schoolId
     * @return
     */
    @Override
    public List<Department> findBatchBySchoolId(Integer schoolId) {

        return departmentMapper.selectBySchoolId(schoolId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/3
     * function: 根据学院id查找学院下的系-专业-班级
     *
     * @param schoolId
     * @return
     */
    @Override
    public List<Department> findDetailBatch(Integer schoolId) {

        return departmentMapper.selectDetailBatch(schoolId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 检查系是否已存在
     *
     * @param department
     * @return true - 存在 false - 不存在
     */
    @Override
    public boolean checkMajorIsExist(Department department) {

        return departmentMapper.checkMajorIsExist(department) != 0;
    }

    /**
     * author: WheelChen
     * date: 2017/10/9
     * function: 根据条件分页模糊查找系
     *
     * @param department
     * @return
     */
    @Override
    public List<Department> findBatchByCondition(Department department) {

        return departmentMapper.selectBatchByCondition(department);
    }

    /**
     * author: WheelChen
     * date: 2017/10/24
     * function: 根据系id级联删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean removeOneChainById(Integer id) {
        //删除系表
        remove(id);
        //删除专业表
        majorService.removeBatchByDepartmentId(id);
        //删除班级表
        classService.removeByDepartmentId(id);
        //查出下的学生表信息
        List<StudentInfo> studentInfoList = studentInfoService.findBatchByDepartmentId(id);

        if (studentInfoList.size() != 0)
            studentService.removeBatchChain(studentInfoList);
        return true;
    }

    /**
     * author: WheelChen
     * date: 2017/11/3
     * function: 根据学校id批量删除
     *
     * @param schoolId 学校id
     * @return
     */
    @Override
    public int removeBatchBySchoolId(Integer schoolId) {
        return departmentMapper.deleteBatchBySchoolId(schoolId);
    }
}
